package com.xcity.game.combat.skill.impl;

import java.util.List;

import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.buff.CombatBuff;
import com.xcity.game.combat.skill.CombatSkill;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.skill.Skill;

/**
 * 不破不立<br/>
 * 消耗自身{0}%生命值提升全员{1}%防御力和{2}%韧性
 * @author yang.li
 *
 */
public class COMBAT_SKILL_90114 extends CombatSkill {

	public COMBAT_SKILL_90114(Skill skill) {
		super(skill);
	}

	@Override
	public void calcDamage(Action action) {
		// 减自己hp
		CombatUnit source = action.unit;
		ActionEffect effect = action.addTarget(source);
		float rate = skill.getParameter(0) / GameStaticConfig.percentBase;
		int hpDelta = Math.max((int) (source.getMaxHp() * rate), 1);
		effect.effect(CombatAttributeType.HP, -hpDelta);
		// 加队友防御、韧性
		List<CombatUnit> units = action.unit.getTeam().all();
		for (int i = 0; i < units.size(); i++) {
			CombatUnit unit = units.get(i);
			if (!unit.isDied()) {
				action.addBuff(new BUFF(skill, unit));
			}
		}
	}
	
	static class BUFF extends CombatBuff {

		public BUFF(Skill skill, CombatUnit owner) {
			super(skill.getTemplate().getCombatBuff(), owner);
			float defense = owner.getDefense();
			float ratio = skill.getParameter(1) / GameStaticConfig.percentBase;
			defense += defense * ratio;
			owner.setAttributeValue(CombatAttributeType.DEFENSE, defense);
			
			float avoidCrit = owner.getAvoidCrit();
			ratio = skill.getParameter(2) / GameStaticConfig.percentBase;
			avoidCrit += avoidCrit * ratio;
			owner.setAttributeValue(CombatAttributeType.AVOID_CRIT, avoidCrit);
		}
		
	}

}
